/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.j4sc.service;


/**
 * Thrown if the SpinVox Create service responds with an error on submission
 * 
 * @author Raj Patel, Dariusz Lorenc
 */
public class SubmissionServiceException extends ServiceException {

	private static final long serialVersionUID = 7381603238483322414L;
	private ServiceError serviceError;
	
	enum ServiceError {
		NoAudio("No-Audio", 400, "No audio file was found within the HTTP request."),
		LongAudio("Long-Audio", 400, "The audio file submitted was too long."),
		Duplicate(400, "The value of <reference> has been received before."),
		Unauthorised(400, "The account ID or user credentials does not match an active account on the API server."),
		MissingHeaders("Missing-Headers", 400, "The HTTP request did not contain all of the expected headers."),
		Invalid(400, "One or more of the values provided in the XML file are invalid."),
		Account(400, "The account profile for specified API account has not been completed."),
		Credit(402, "The account has no remaining conversion credits available."),
		UnsupportedAudio("Unsupported-Audio", 415, "SpinVox API service unavailable. Your application should retry the request later."),
		Server(500, "An internal SpinVox API Server error has occurred. Your application should retry the request later."),
		Unavailable(503, "SpinVox API service unavailable. Your application should retry the request later."),
		Throttle(503, "The account maximum throughput rate has been exceeded.");
		
		final int status;
		private final String description;
		private final String error;

		private ServiceError(int status, String description) {
			this.error = this.toString(); // name of enum constant same as X-Error header value
			this.status = status;
			this.description = description;
		}
		
		private ServiceError(String error, int status, String description) {
			this.error = error;
			this.status = status;
			this.description = description;
		}
	}

	/**
	 * Construct a new instance using the ServiceError
	 * 
	 * @param serviceError
	 */
	public SubmissionServiceException(ServiceError serviceError) {
		super(serviceError.status, String.format("Error [%s] Description[%s]", serviceError.error, serviceError.description));
		this.serviceError = serviceError;
	}
	
	/**
	 * The X-Error header value as returned by the service
	 * 
	 * @return the X-Error header value
	 */
	public String getError() {
		return serviceError.error;
	}
}
